<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * @version $Id: OxidPlugin.php 268 2013-03-22 06:33:38Z jobarthel@gmail.com $
 * 
 * @category Piwik_Plugins
 * @package Piwik_OXID_Plugin
 */


require_once PIWIK_INCLUDE_PATH . '/plugins/OxidPlugin/functions.php';


/**
 *
 * @package Piwik_OxidPlugin
 */
class Piwik_OxidPlugin extends Piwik_Plugin
{
	/**
	 * Return information about this plugin.
	 *
	 * @see Piwik_Plugin
	 *
	 * @return array
	 */
	public function getInformation()
	{
		return array(
			'description' => Piwik_Translate('OxidPlugin_PluginDescription'),
			'homepage' => 'http://piwik.org/',
			'author' => 'Joachim Barthel',
			'author_homepage' => 'http://code.google.com/p/piwik-oxid-plugin/',
			'license' => 'GPL v3 or later',
			'license_homepage' => 'http://www.gnu.org/licenses/gpl.html',
			'version' => '0.11',
			'translationAvailable' => true,
		);
	}
	
	
	
	function getListHooksRegistered()
	{
		$hooks = array(
			'Menu.add' => 'addMenus',
		);
		return $hooks;
	}

	function addMenus()
	{
                $iSite = Piwik_Common::getRequestVar('idSite');
                if ($this->EnableMenuRevenue[$iSite])        $menus[Piwik_Translate('OxidPlugin_Revenue')] = 'oxidMenuRevenue';
                if ($this->EnableMenuTimeRevenue[$iSite])    $menus[Piwik_Translate('OxidPlugin_TimeRevenue')] = 'oxidMenuTimeRevenue';
		if ($this->EnableMenuReadyToSend[$iSite])    $menus[Piwik_Translate('OxidPlugin_ReadyToSend')] = 'oxidMenuReadyToSend';
		if ($this->EnableMenuCIAnotPaid[$iSite])     $menus[Piwik_Translate('OxidPlugin_CIAnotPaid')] = 'oxidMenuCIAnotPaid';
		if ($this->EnableMenuCODnotReceived[$iSite]) $menus[Piwik_Translate('OxidPlugin_CODnotReceived')] = 'oxidMenuCODnotReceived';
		if ($this->EnableMenuInvoiceNotPaid[$iSite]) $menus[Piwik_Translate('OxidPlugin_InvoiceNotPaid')] = 'oxidMenuInvoiceNotPaid';
		if ($this->EnableMenuPaidInAdvance[$iSite])  $menus[Piwik_Translate('OxidPlugin_Prepaid')] = 'oxidMenuPaidInAdvance';
		if ($this->EnableMenuTopSeller[$iSite])      $menus[Piwik_Translate('OxidPlugin_TopSeller')] = 'oxidMenuTopSeller';
		if ($this->EnableMenuTopCancels[$iSite])     $menus[Piwik_Translate('OxidPlugin_TopCancels')] = 'oxidMenuTopCancels';
		if ($this->EnableMenuAgeAnalysis[$iSite])    $menus[Piwik_Translate('OxidPlugin_AgeAnalysis')] = 'oxidMenuAgeAnalysis';
		if ($this->EnableMenuStoreStatus[$iSite])    $menus[Piwik_Translate('OxidPlugin_StoreStatus')] = 'oxidMenuStoreStatus';
		if ($this->EnableMenuManufacturerRevenue[$iSite])	$menus[Piwik_Translate('OxidPlugin_ManufacturerRevenue')] = 'oxidMenuManufacturerRevenue';
		if ($this->EnableMenuVendorRevenue[$iSite])  $menus[Piwik_Translate('OxidPlugin_VendorRevenue')] = 'oxidMenuVendorRevenue';
		if (oxFeedbackInstalled($this))      $menus[Piwik_Translate('OxidPlugin_Feedback')] = 'oxidMenuFeedbackList';
		

		Piwik_AddMenu('OXID eShop', '', array('module' => 'OxidPlugin', 'action' => 'oxidMenuRevenue'), true, 30);
		$order = 1;
		foreach($menus as $subMenu => $action) 
		{
			Piwik_AddMenu('OXID eShop', $subMenu, array('module' => 'OxidPlugin', 'action' => $action), true, $order++);
		}
	}	

	
	
	function postLoad()
	{
		// we register the widgets so they appear in the "Add a new widget" window in the dashboard
		// Note that the first two parameters can be either a normal string, or an index to a translation string
		include PIWIK_INCLUDE_PATH . '/plugins/OxidPlugin/conf/'.'config.inc.php';
		
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_revenueWidget', 'OxidPlugin', 'revenueWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_birthdayWidget', 'OxidPlugin', 'birthdayWidget');
		if (oxFeedbackInstalled($this) == TRUE)
			Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_feedbackWidget', 'OxidPlugin', 'feedbackWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_ppnotpaidWidget', 'OxidPlugin', 'ppnotpaidWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_codnotpaidWidget', 'OxidPlugin', 'codnotpaidWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_invoicenotpaidWidget', 'OxidPlugin', 'invoicenotpaidWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_paytypeSumsWidget', 'OxidPlugin', 'paytypeSumsWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_paymentWidget', 'OxidPlugin', 'paymentWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_userAgeWidget', 'OxidPlugin', 'userAgeWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_newsregWidget', 'OxidPlugin', 'newsregWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_rateandreviewWidget', 'OxidPlugin', 'rateandreviewWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_logisticsWidget', 'OxidPlugin', 'logisticsWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_voucherUseWidget', 'OxidPlugin', 'voucherUseWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_voucherOverviewWidget', 'OxidPlugin', 'voucherOverviewWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_liveRevenueWidget', 'OxidPlugin', 'liveRevenueWidget');
		Piwik_AddWidget('OxidPlugin_oxidWidgets', 'OxidPlugin_revenueAlertWidget', 'OxidPlugin', 'revenueAlertWidget');
	}
}

/**
 *
 * @package Piwik_OxidPlugin
 */
//class Piwik_OxidPlugin_Controller extends Piwik_Controller
//{	
	
	
	/**
	 * This method displays a text containing an help about "How to build plugins for Piwik".
	 * This help is then used on http://dev.piwik.org/trac/wiki/Plugins/GlobalFunctions
	 *
	 */
	/*function index()
	{
		$out = '';
		$out .= '<i>This page aims to list the different functions you can use when programming plugins for Piwik.</i><br />';
		$out .= '<b>Be careful, the following APIs may change in the near future as Piwik is still in development.</b><br />';

		$out .= '<h2>General</h2>';
		$out .= '<h3>Accessible from your plugin controller</h3>';
		
		$out .= '<code>$this->date</code> = current selected <b>Piwik_Date</b> object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Date.php">class</a>)<br />';
		$out .= '<code>$period = Piwik_Common::getRequestVar("period");</code> - Get the current selected period<br />';
		$out .= '<code>$idSite = Piwik_Common::getRequestVar("idSite");</code> - Get the selected idSite<br />';
		$out .= '<code>$site = new Piwik_Site($idSite);</code> - Build the Piwik_Site object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Site.php">class</a>)<br />';
		$out .= '<code>$this->str_date</code> = current selected date in YYYY-MM-DD format<br />';
		
		$out .= '<h3>Misc</h3>';
		$out .= '<code>Piwik_AddMenu( $mainMenuName, $subMenuName, $url );</code> - Adds an entry to the menu in the Piwik interface (See the example in the <a href="http://dev.piwik.org/trac/browser/tags/1.0/plugins/UserCountry/UserCountry.php#L76">UserCountry Plugin file</a>)<br />';
		$out .= '<code>Piwik_AddWidget( $widgetCategory, $widgetName, $controllerName, $controllerAction, $customParameters = array());</code> - Adds a widget that users can add in the dashboard, or export using the Widgets link at the top of the screen. See the example in the <a href="http://dev.piwik.org/trac/browser/tags/1.0/plugins/UserCountry/UserCountry.php#L70">UserCountry Plugin file</a> or any other plugin)<br />';
		$out .= '<code>Piwik_Common::prefixTable("site")</code> = <b>' . Piwik_Common::prefixTable("site") . '</b><br />';
		
		
		$out .= '<h2>User access</h2>';
		$out .= '<code>Piwik::getCurrentUserLogin()</code> = <b>' . Piwik::getCurrentUserLogin() . '</b><br />';
		$out .= '<code>Piwik::isUserHasSomeAdminAccess()</code> = <b>' . self::boolToString(Piwik::isUserHasSomeAdminAccess()) . '</b><br />';
		$out .= '<code>Piwik::isUserHasAdminAccess( array $idSites = array(1,2) )</code> = <b>' . self::boolToString(Piwik::isUserHasAdminAccess(array(1,2) )) . '</b><br />';
		$out .= '<code>Piwik::isUserHasViewAccess( array $idSites = array(1) ) </code> = <b>' . self::boolToString(Piwik::isUserHasViewAccess(array(1))) . '</b><br />';
		$out .= '<code>Piwik::isUserIsSuperUser()</code> = <b>' . self::boolToString(Piwik::isUserIsSuperUser()) . '</b><br />';
		
		$out .= '<h2>Execute SQL queries</h2>';
		$txtQuery = "SELECT token_auth FROM ".Piwik_Common::prefixTable('user')." WHERE login = ?";
		$result = Piwik_FetchOne($txtQuery, array('anonymous'));
		$out .= '<code>Piwik_FetchOne("'.$txtQuery.'", array("anonymous"))</code> = <b>' . var_export($result,true) . '</b><br />';
		$out .= '<br />';
		
		$query = Piwik_Query($txtQuery, array('anonymous'));
		$fetched = $query->fetch();
		$token_auth = $fetched['token_auth'];
		
		$out .= '<code>$query = Piwik_Query("'.$txtQuery.'", array("anonymous"))</code><br />';
		$out .= '<code>$fetched = $query->fetch();</code><br />';
		$out .= 'At this point, we have: <code>$fetched[\'token_auth\'] == <b>'.var_export($token_auth,true) . '</b></code><br />';
		
  		$out .= '<h2>Example Sites information API</h2>';
		$out .= '<code>Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess(),true) . '</pre></b><br />';
		$out .= '<code>Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(),true) . '</pre></b><br />';

		$out .= '<h2>Example API  Users information</h2>';
		$out .= 'View the list of API methods you can call on <a href="http://dev.piwik.org/trac/wiki/API/Reference#Methods">API reference</a><br />';
		$out .= 'For example you can try <code>Piwik_UsersManager_API::getInstance()->getUsersSitesFromAccess("view");</code> or <code>Piwik_UsersManager_API::getInstance()->deleteUser("userToDelete");</code><br />';
		
		$out .= '<h2>Javascript in Piwik</h2>';
		$out .= '<h3>i18n internationalization</h3>';
		$out .= 'In order to translate strings within Javascript code, you can use the javascript function _pk_translate( token );.
				<ul><li>The "token" parameter is the string unique key found in the translation file. For this token string to be available in Javascript, you must
				suffix your token by "_js" in the language file. For example, you can add <code>\'Goals_AddGoal_js\' => \'Add Goal\',</code> in the lang/en.php file</li>
				<li>You then need to instruct Piwik to load your Javascript translations for your plugin; by default, all translation strings are not loaded in Javascript for performance reasons. This can be done by calling a custom-made Smarty modifier before the Javascript code requiring translations, eg. 
					<code>{loadJavascriptTranslations plugins=\'$YOUR_PLUGIN_NAME\'}</code>. In our previous example, the $YOUR_PLUGIN_NAME being Goals, we would write <code>{loadJavascriptTranslations plugins=\'Goals\'}</code>
					</li><li>You can then print this string from your JS code by doing <code>_pk_translate(\'Goals_AddGoal_js\');</code>.
					</li></ul>';

		$out .= '<h3>Reload a widget in the dashboard</h3>';
		$out .= 'It is sometimes useful to reload one widget in the dashboard (for example, every 20 seconds for a real time widget, or after a setting change). 
					You can easily force your widget to reload in the dashboard by calling the helper function <code>piwik.dashboardObject.reloadEnclosingWidget($(this));</code>.';
		
		$out .= '<h2>Smarty plugins</h2>';
		$out .= 'There are some builtin plugins for Smarty especially developped for Piwik. <br />
				You can find them on the <a href="http://dev.piwik.org/trac/browser/trunk/core/SmartyPlugins">SVN at /trunk/core/SmartyPlugins</a>. <br />
				More documentation to come about smarty plugins.<br />';
		
		echo $out;
	}
	
	static private function boolToString($bool)
	{
		return $bool ? "true" : "false";
	}
}*/
